//
//  JSONObject.m
//  Sunshine
//
//  Created by usera on 16/4/14.
//  Copyright © 2016年 usera. All rights reserved.
//
//  参数NSJSONWritingPrettyPrinted的意思是将生成的json数据格式化输出，这样可读性高，不设置则输出的json字符串就是一整行。
//  SJSONReadingMutableLeaves：返回的JSON对象中字符串的值为NSMutableString

#import "JsonObject.h"

@implementation JsonObject


//-----字典/数组转json字符串------
+ (NSString *)stringWithObject:(id)obj {
    if (obj == nil || [obj isKindOfClass:[NSNull class]]) {
        return @"";
    }else{
        NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:0 error:nil];
        return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    }
}

//-----json字符串转字典------
+ (NSDictionary *)dictionaryWithString:(NSString *)obj {
    if (obj == nil || [obj isKindOfClass:[NSNull class]]) {
        return [[NSDictionary alloc] init];
    } else {
        NSData *data = [obj dataUsingEncoding:NSUTF8StringEncoding];
        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        return [[NSDictionary alloc] initWithDictionary:jsonObject];
    }
}

//-----json字符串转数组------
+ (NSArray *)arrayWithString:(NSString *)obj {
    if (obj == nil || [obj isKindOfClass:[NSNull class]]) {
        return [[NSArray alloc] init];
    } else {
        NSData *data = [obj dataUsingEncoding:NSUTF8StringEncoding];
        id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        return [[NSArray alloc] initWithArray:jsonObject];
    }
}

//-----json字符串转字典/数组------
+ (id)objectWithString:(NSString *)obj {
    if (obj == nil || [obj isKindOfClass:[NSNull class]]) {
        return nil;
    }else {
        NSData *data = [obj dataUsingEncoding:NSUTF8StringEncoding];
        return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];;
    }
}

//判断是否为空
+ (NSString*)fuckNULL:(NSObject *)obj {
    if (obj == nil || [obj isKindOfClass:[NSNull class]]){
        return @"";
    }else if ([obj isKindOfClass:[NSNumber class]]) {
        return [NSString stringWithFormat:@"%@",obj];
    }else{
        return [NSString stringWithFormat:@"%@",obj];
    }
}

//把NSDictionary解析成post格式的NSString字符串
+ (NSString *)parseParams:(NSDictionary *)dict {
    NSString *keyValueFormat;
    NSMutableString *result = [NSMutableString new];
    NSMutableArray *array = [NSMutableArray new];
    //实例化一个key枚举器用来存放dictionary的key
    NSEnumerator *keyEnum = [dict keyEnumerator];
    id key;
    while (key = [keyEnum nextObject]) {
        keyValueFormat = [NSString stringWithFormat:@"%@=%@&",key,[dict valueForKey:key]];
        [result appendString:keyValueFormat];
        [array addObject:keyValueFormat];
    }
    return result;
}

//数组拆分字符串
+ (NSString *)componentsStringWithArray:(NSArray *)array by:(NSString *)str {
    if (array == nil || array.count == 0){
        return @"";
    }else {
        return [array componentsJoinedByString:str];
    }
}

//字符分割数组
+ (NSArray *)componentsArrayWithString:(NSString *)string by:(NSString *)str {
    if (string == nil || string.length == 0){
        return [[NSArray alloc] init];
    }else{
        return [string componentsSeparatedByString:str];
    }
}

@end
